package org.example.myAop.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.cj.jdbc.Driver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author 莫磊
 * @date 2025/7/30
 * @description
 */
@Configuration
@ComponentScan("org.example.myAop")
// 开启AOP代理扫描，使用CGLIB
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement
public class Spring6Config {

	@Bean
	public JdbcTemplate jdbcTemplate(DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}


	@Bean
	public DataSource getDataSource() throws SQLException {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setUsername("root");
		druidDataSource.setDriver(new Driver());
		druidDataSource.setPassword("molei");
		druidDataSource.setUrl("jdbc:mysql://localhost:3306/spring6?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8");
		return druidDataSource;
	}

	@Bean
	public TransactionManager getTransactionManager() throws SQLException {
		return new DataSourceTransactionManager(getDataSource());
	}
}
